import {
  createRouter,
  createWebHistory // 历史模式  路由地址不带 #
  // createWebHashHistory // hash模式  路由地址带 #
} from 'vue-router'
import { useStore } from '@/stores'

import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
// 隐藏右上角环形图标
Nprogress.configure({
  showSpinner: false
})
// createRouter 创建路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login/index.vue'),
      meta: { title: '登录' }
    },
    {
      path: '/',
      component: () => import('@/views/layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: () => import('@/views/home/index.vue'),
          meta: { title: '首页' }
        },
        {
          path: '/article',
          component: () => import('@/views/article/index.vue'),
          meta: { title: '健康百科' }
        },
        {
          path: '/notify',
          component: () => import('@/views/notify/index.vue'),
          meta: { title: '消息通知' }
        },
        {
          path: '/user',
          component: () => import('@/views/user/index.vue'),
          meta: { title: '个人中心' }
        }
      ]
    },
    {
      path: '/user/patient',
      component: () => import('@/views/user/PatientPage.vue'),
      meta: { title: '家庭档案' }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/consult/ConsultFast.vue'),
      meta: { title: '极速问诊' }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/consult/ConsultDep.vue'),
      meta: { title: '选择科室' }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/consult/ConsultIllness.vue'),
      meta: { title: '病情描述' }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/consult/ConsultPay.vue'),
      meta: { title: '问诊支付' }
    },
    {
      path: '/room',
      component: () => import('@/views/room/index.vue'),
      meta: { title: '问诊室' },
      // 路由独享的守卫
      beforeEnter(to) {
        // 进入这个页面前  如果payResult 是false 强制去问诊记录页面
        if (to.query.payResult === 'false') return '/user/consult'
      }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/user/ConsultPage.vue'),
      meta: { title: '问诊记录' }
    },
    {
      path: '/user/consult/:id',
      component: () => import('@/views/user/ConsultDetail.vue'),
      meta: { title: '问诊详情' }
    },
    {
      path: '/order/pay',
      component: () => import('@/views/order/OrderPay.vue'),
      meta: { title: '药品支付' }
    },

    {
      path: '/order/pay/result',
      component: () => import('@/views/order/PayResult.vue'),
      meta: { title: '支付结果' }
    },
    {
      path: '/order/:id',
      component: () => import('@/views/order/OrderDetail.vue'),
      meta: { title: '药品订单详情' }
    },
    {
      path: '/order/logistics/:id',
      component: () => import('@/views/order/OrderLogistics.vue'),
      meta: { title: '物流详情' }
    },
    {
      path: '/login/callback',
      component: () => import('@/views/login/login-callback.vue'),
      meta: { title: '登录回跳' }
    }
  ]
})
export default router
const whiteList = ['/login', '/login/callback']
// 前置导航守卫
router.beforeEach((to) => {
  Nprogress.start() // 开启进度条
  const { user } = useStore()
  const token = user.user?.token
  if (!token && !whiteList.includes(to.path)) return '/login'
  // return true  或者不写 表示放行
})

// 后置导航守卫
router.afterEach((to) => {
  Nprogress.done() // 关闭进度条
  document.title = import.meta.env.VITE_APP_TITLE + to.meta.title
})
